package com.acme.bankapp.service.network;

import java.io.*;
import java.net.*;

/**
 * Class provides bank client functionality
 * @author alex
 */
public class BankClient extends BankNetwork {
	/**
	 * The main method of class
	 * @param args the command-line parameters
	 */
	public static void main(String[] args) {
		(new BankClient()).startClient();
	}
	
	/**
	 * Start client
	 */
	public void startClient() {
		try (Socket socket = new Socket(BankNetwork.SERVER_HOST, BankNetwork.SERVER_PORT);
				BufferedReader tempReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				BufferedWriter tempWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
			
			this.reader = tempReader;
			this.writer = tempWriter;
			System.out.println("==Connected to server==");
			
			sendMessage(BankNetwork.COMMAND_ADD_CLIENT);
			sendMessage("accounttype=c;balance=3;overdraft=3;name=Client 1;gender=f;");
			recieveMessage();
			
			sendMessage(BankNetwork.COMMAND_ADD_CLIENT);
			sendMessage("accounttype=c;balance=3;overdraft=3;name=Client 2;gender=f;");
			recieveMessage();
			
			sendMessage(BankNetwork.COMMAND_ADD_CLIENT);
			sendMessage("accounttype=c;balance=3;overdraft=3;name=Client 2;gender=f;");
			recieveMessage();
			
			sendMessage(BankNetwork.COMMAND_LIST_CLIENTS);
			String message;
			do {
				message = recieveMessage();
			} while (!message.equals(BankNetwork.COMMAND_STOP));
			
			sendMessage(BankNetwork.COMMAND_EXIT);
		} catch (IOException e) {
			System.out.println("Err: Network error!");
		}
	}
}
